//Yujie, Mar.4.2011
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;


public class CS576_HW2_Part2_Yujie {
	
	static int width = 352;
	static int height = 288;	

   public static void main(String[] args) {
   	

	String fileName = args[0];
	
	int n = Integer.parseInt(args[1]);
	int m = Integer.parseInt(args[2]);
	if(n > 16777216) 
	{
		System.out.println("Error! n should be <= 16777216.");
		System.exit(-1);
	}
	
	if (m == 1)
	{
		BufferedImage myImgInput = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //TYPE_INT_RGB, Represents an image with 8-bit RGB color components packed into integer pixels.
		BufferedImage myImgOutput1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
		//BufferedImage myImgOutput1 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); //test
		BufferedImage myImgOutputR = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
		BufferedImage myImgOutputG = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
		BufferedImage myImgOutputB = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
	    try {
		    File file = new File(fileName);
		    InputStream is = new FileInputStream(file);

		    long len = file.length();
		    byte[] bytes = new byte[(int)len];
		    int[][][] myRGBArrayIn = new int[352][288][3];
		    
		    int offset = 0;
	        int numRead = 0;
	        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
	            offset += numRead;
	        }
	        
	        //Step.1 consider the space of all color vectors.
	        double radical = Math.pow(n, 1/3.0);
			int r_cells = 0;
			int g_cells = 0;
			int b_cells = 0;
			r_cells = (int)radical;
			g_cells = (int)radical;
			b_cells = (int)radical;
			
			System.out.println("-- Yujie Hao --\nm == 1\nBefore processing:\nn = " + n + ", radical = " + radical + ", round = "+ Math.round(radical));		
			System.out.println("r_cells = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			
			int i = 0;
			boolean revised = false;
			while(r_cells * g_cells * b_cells < n)
			{
				revised = true;
				if(i > 2) i = 0;
				switch(i)
				{
					case 0: r_cells++;
						break;
					case 1: g_cells++;
						break;
					case 2: b_cells++;
						break;
					default:
						break;
				}
				i++;				
			}
			
			if(revised == true)
			System.out.println("\n\nAfter r_cells/g_cells/b_cells++:\nr = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			
			if(r_cells * g_cells * b_cells > n)
			{
				r_cells--;
				System.out.println("\n\nAfter r_cells--:\nr = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			}		
			
			int[] sizeOfVectorSpace = new int[3];
			sizeOfVectorSpace[0] = r_cells;
			sizeOfVectorSpace[1] = g_cells;
			sizeOfVectorSpace[2] = b_cells;    
	    		
			// read input image:
			int ind = 0;
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayIn[x][y][0] = 0x000000ff & bytes[ind];						//r
					myRGBArrayIn[x][y][1] = 0x000000ff & bytes[ind + height * width];		//g
					myRGBArrayIn[x][y][2] = 0x000000ff & bytes[ind + height * width * 2];	//b
					
					ind++;
				}
			}
			
			// display input image
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					int pix = 0xff000000 | ((myRGBArrayIn[x][y][0] & 0xff) << 16) | ((myRGBArrayIn[x][y][1] & 0xff) << 8) | (myRGBArrayIn[x][y][2] & 0xff);//good
					//int pix = ((a << 24) + (r << 16) + (g << 8) + b);
					myImgInput.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					ind++;
				}
			}
			
			// Step2. Create code book.
			int[] rgb_interval = new int[3];
			rgb_interval[0] = 256 / sizeOfVectorSpace[0];//r
			rgb_interval[1] = 256 / sizeOfVectorSpace[1];//g
			rgb_interval[2] = 256 / sizeOfVectorSpace[2];//b
			
			//System.out.println(rgb_interval[0]);
			//Step3. Create index
			int[][][] myRGBIndex = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBIndex[x][y][0] = myRGBArrayIn[x][y][0]/rgb_interval[0];						//r_index
					if(myRGBIndex[x][y][0] * rgb_interval[0] > 255)
					{
						myRGBIndex[x][y][0]--;
					}
					myRGBIndex[x][y][1] = myRGBArrayIn[x][y][1]/rgb_interval[1];						//g_index
					if(myRGBIndex[x][y][1] * rgb_interval[1] > 255)
					{
						myRGBIndex[x][y][1]--;
					}
					myRGBIndex[x][y][2] = myRGBArrayIn[x][y][2]/rgb_interval[2];						//b_index	
					if(myRGBIndex[x][y][2] * rgb_interval[2] > 255)
					{
						myRGBIndex[x][y][2]--;
					}
				}
			}
			
			//Step4. Output the images. Look up the codebook
			int[][][] myRGBArrayOut = new int[352][288][3];
			
			
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayOut[x][y][0] = myRGBIndex[x][y][0] * rgb_interval[0] + rgb_interval[0]/2;//r
					if(myRGBArrayOut[x][y][0] < 0) myRGBArrayOut[x][y][0] = 0;
					if(myRGBArrayOut[x][y][0] > 255) myRGBArrayOut[x][y][0] = 255;
					myRGBArrayOut[x][y][1] = myRGBIndex[x][y][1] * rgb_interval[1] + rgb_interval[1]/2;//g
					if(myRGBArrayOut[x][y][1] < 0) myRGBArrayOut[x][y][1] = 0;
					if(myRGBArrayOut[x][y][1] > 255) myRGBArrayOut[x][y][1] = 255;
					myRGBArrayOut[x][y][2] = myRGBIndex[x][y][2] * rgb_interval[2] + rgb_interval[2]/2;//b
					if(myRGBArrayOut[x][y][2] < 0) myRGBArrayOut[x][y][2] = 0;
					if(myRGBArrayOut[x][y][2] > 255) myRGBArrayOut[x][y][2] = 255;
				}
			}		
			// display output image1			
			ind = 0;
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					//int pix = 0xff000000 | ((myRGBArrayOut[x][y][0] & 0xff) << 16) | ((myRGBArrayOut[x][y][1] & 0xff) << 8) | (myRGBArrayOut[x][y][2] & 0xff);NOT USE,3/4/2011
					int pix = ((a << 24) + (myRGBArrayOut[x][y][0] << 16) + (myRGBArrayOut[x][y][1] << 8) + myRGBArrayOut[x][y][2]);// good	
					//int pix = ((a << 24) + (myRGBArrayOut[x][y][0] << 16) + (myRGBArrayOut[x][y][1] << 8) + myRGBArrayOut[x][y][2]);// test
					//int pix = 0;//black
					myImgOutput1.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					ind++;
				}
			}
			
			//display output image2, red difference, smallest, so use less cells for red
			int[][][] myRGBArrayR = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					System.out.println(Math.abs((myRGBIndex[x][y][0] * rgb_interval[0] + rgb_interval[0]/2) - myRGBArrayIn[x][y][0]));
					myRGBArrayR[x][y][0] = 255 - Math.abs((myRGBIndex[x][y][0] * rgb_interval[0] + rgb_interval[0]/2) - myRGBArrayIn[x][y][0]);//r
					if(myRGBArrayR[x][y][0] > 255) myRGBArrayR[x][y][0] = 255;
					myRGBArrayR[x][y][1] = myRGBArrayR[x][y][0];//g
					myRGBArrayR[x][y][2] = myRGBArrayR[x][y][0];//b
				}
			}			
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					//int pix = 0xff000000 | ((myRGBArrayR[x][y][0] & 0xff) << 16) | ((myRGBArrayR[x][y][1] & 0xff) << 8) | (myRGBArrayR[x][y][2] & 0xff);NOT USE,3/4/2011
					int pix = ((a << 24) + (myRGBArrayR[x][y][0] << 16) + (myRGBArrayR[x][y][1] << 8) + myRGBArrayR[x][y][2]);// good	
					//int pix = 0;//black
					myImgOutputR.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					
				}
			}
			
			//display output image3, green difference
			int[][][] myRGBArrayG = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayG[x][y][0] = 0;//r
					myRGBArrayG[x][y][1] = Math.abs((myRGBIndex[x][y][1] * rgb_interval[1] + rgb_interval[1]/2) - myRGBArrayIn[x][y][1]);//G
					if(myRGBArrayG[x][y][1] > 255) myRGBArrayG[x][y][1] = 255;
					myRGBArrayG[x][y][2] = 0;//b
				}
			}
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					int pix = ((a << 24) + (myRGBArrayG[x][y][0] << 16) + (myRGBArrayG[x][y][1] << 8) + myRGBArrayG[x][y][2]);
					myImgOutputG.setRGB(x, y, pix);
				}
			}
			
			//display output image4, blue difference
			int[][][] myRGBArrayB = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{
					myRGBArrayB[x][y][0] = 0;//r
					myRGBArrayB[x][y][1] = 0;//g
					//myRGBArrayB[x][y][2] = (myRGBIndex[x][y][2] * rgb_interval[2] + rgb_interval[2]/2) - myRGBArrayIn[x][y][2];//B
					myRGBArrayB[x][y][2] = myRGBArrayIn[x][y][2] - (myRGBIndex[x][y][2] * rgb_interval[2]);//B
					//if(myRGBArrayB[x][y][2] > 255) myRGBArrayB[x][y][2] = 255;
				}
			}
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					int pix = ((a << 24) + (myRGBArrayB[x][y][0] << 16) + (myRGBArrayB[x][y][1] << 8) + myRGBArrayB[x][y][2]);
					myImgOutputB.setRGB(x, y, pix);
				}
			}
			
			is.close();
			
		
			
			
	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	    
	    // Use a label to display the image
	    //JFrame frame = new JFrame();
	    //JLabel label = new JLabel(new ImageIcon(img));
	    JFrame myFrameInput = new JFrame("Input frame");	    
	    JLabel myLabelInput = new JLabel(new ImageIcon(myImgInput));
	    JFrame myFrameOutput1 = new JFrame("Output frame1");
	    JLabel myLabelOutput1 = new JLabel(new ImageIcon(myImgOutput1));
	    JFrame myFrameOutputR = new JFrame("Output frameR");
	    JLabel myLabelOutputR = new JLabel(new ImageIcon(myImgOutputR));
	    JFrame myFrameOutputG = new JFrame("Output frameG");
	    JLabel myLabelOutputG = new JLabel(new ImageIcon(myImgOutputG));
	    JFrame myFrameOutputB = new JFrame("Output frameB");
	    JLabel myLabelOutputB = new JLabel(new ImageIcon(myImgOutputB));
	    
	    //frame.getContentPane().add(label, BorderLayout.CENTER); // using JFrame you need to add the child to the JFrame's content pane:
																//	frame.getContentPane().add(child);
	    //frame.pack(); //Size the frame.
	    //frame.setVisible(true); //Show it.
	    myFrameInput.getContentPane().add(myLabelInput, BorderLayout.CENTER);
	    myFrameInput.pack(); //Size the frame.
	    myFrameInput.setVisible(true); //Show it.
	    
	    myFrameOutput1.getContentPane().add(myLabelOutput1, BorderLayout.CENTER);
	    myFrameOutput1.pack();
	    //myFrameOutput1.setLocation(400, 0);
	    myFrameOutput1.setVisible(true);
	    
	    myFrameOutputR.getContentPane().add(myLabelOutputR, BorderLayout.CENTER);
	    myFrameOutputR.pack();
	    myFrameOutputR.setLocation(400, 0);
	    myFrameOutputR.setVisible(true);
	    
	    myFrameOutputG.getContentPane().add(myLabelOutputG, BorderLayout.CENTER);
	    myFrameOutputG.pack();
	    myFrameOutputG.setLocation(0, 350);
	    myFrameOutputG.setVisible(true);
	    
	    myFrameOutputB.getContentPane().add(myLabelOutputB, BorderLayout.CENTER);
	    myFrameOutputB.pack();
	    myFrameOutputB.setLocation(400, 350);
	    myFrameOutputB.setVisible(true);
	}
	
	if (m == 2)
	{
		System.out.println("m == 2:\n");
		BufferedImage myImgInput = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //TYPE_INT_RGB, Represents an image with 8-bit RGB color components packed into integer pixels.
		BufferedImage myImgOutput1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 	
		BufferedImage myImgOutputR = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
		BufferedImage myImgOutputG = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
		BufferedImage myImgOutputB = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
	    try {
		    File file = new File(fileName);
		    InputStream is = new FileInputStream(file);

		    long len = file.length();
		    byte[] bytes = new byte[(int)len];
		    int[][][] myRGBArrayIn = new int[352][288][3];
		    
		    int offset = 0;
	        int numRead = 0;
	        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
	            offset += numRead;
	        }
	        
	        //Step.1 consider the space of all color vectors.
	        double radical = Math.pow(n, 1/3.0);
			int r_cells = 0;
			int g_cells = 0;
			int b_cells = 0;
			r_cells = (int)radical;
			g_cells = (int)radical;
			b_cells = (int)radical;
			
			System.out.println("Before processing:\nn = " + n + ", radical = " + radical + ", round = "+ Math.round(radical));		
			System.out.println("r_cells = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			
			int i = 0;
			boolean revised = false;
			while(r_cells * g_cells * b_cells < n)
			{
				revised = true;
				if(i > 2) i = 0;
				switch(i)
				{
					case 0: r_cells++;
						break;
					case 1: g_cells++;
						break;
					case 2: b_cells++;
						break;
					default:
						break;
				}
				i++;				
			}
			
			if(revised == true)
			System.out.println("\n\nAfter r_cells/g_cells/b_cells++:\nr = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			
			if(r_cells * g_cells * b_cells > n)
			{
				r_cells--;
				System.out.println("\n\nAfter r_cells--:\nr = " + r_cells + ", g_cells = " + g_cells + ", b_cells = " + b_cells + ", r_cells*g_cells*b_cells = " + r_cells * g_cells * b_cells);
			}		
			
			int[] sizeOfVectorSpace = new int[3];
			sizeOfVectorSpace[0] = r_cells;
			sizeOfVectorSpace[1] = g_cells;
			sizeOfVectorSpace[2] = b_cells;    
	    		
			// read input image:
			int ind = 0;
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayIn[x][y][0] = 0x000000ff & bytes[ind];					//r
					myRGBArrayIn[x][y][1] = 0x000000ff & bytes[ind + height * width];	//g
					myRGBArrayIn[x][y][2] = 0x000000ff & bytes[ind + height * width * 2];//b
					
					ind++;
				}
			}
			
			// display input image
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					int pix = 0xff000000 | ((myRGBArrayIn[x][y][0] & 0xff) << 16) | ((myRGBArrayIn[x][y][1] & 0xff) << 8) | (myRGBArrayIn[x][y][2] & 0xff);
					//int pix = ((a << 24) + (r << 16) + (g << 8) + b);
					myImgInput.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					ind++;
				}
			}
			
			// Step2. Create code book.
			int[] rgb_interval = new int[3];
			rgb_interval[0] = 256 / sizeOfVectorSpace[0];//r
			rgb_interval[1] = 256 / sizeOfVectorSpace[1];//g
			rgb_interval[2] = 256 / sizeOfVectorSpace[2];//b
			
			//System.out.println(rgb_interval[0]);
			//Step3. Create original index
			int[][][] myRGBIndex = new int[352][288][3];
			int[] indexAmountArrayR = new int[sizeOfVectorSpace[0]];
			int[] indexAmountArrayG = new int[sizeOfVectorSpace[1]];
			int[] indexAmountArrayB = new int[sizeOfVectorSpace[2]];
			for(int j = 0; i < indexAmountArrayR.length; i++)
			{
				indexAmountArrayR[j] = 0;
			}
			for(int j = 0; i < indexAmountArrayG.length; i++)
			{
				indexAmountArrayG[j] = 0;
			}
			for(int j = 0; i < indexAmountArrayB.length; i++)
			{
				indexAmountArrayB[j] = 0;
			}
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{				
					//System.out.println(myRGBArrayIn[x][y][0]/rgb_interval[0]);
					indexAmountArrayR[(myRGBArrayIn[x][y][0]/rgb_interval[0])]++;
					indexAmountArrayG[(myRGBArrayIn[x][y][1]/rgb_interval[1])]++;
					indexAmountArrayB[(myRGBArrayIn[x][y][2]/rgb_interval[2])]++;
					
					
				}
			}
			
			//search for R min index and max index:
			int maxIndexR = indexAmountArrayR[0];			
			int minIndexR = indexAmountArrayR[0];
			int maxIndexRin = 0;
			int minIndexRin = 0;
			for(int j = 0; j < sizeOfVectorSpace[0]; j++)
			{
				if (maxIndexR < indexAmountArrayR[j]) 
				{
					maxIndexR = indexAmountArrayR[j];
					maxIndexRin = j;
				}
					
				if (minIndexR > indexAmountArrayR[j]) 
				{
					minIndexR = indexAmountArrayR[j];
					minIndexRin = j;
				}
					
			}
			System.out.println("maxIndexR amount = " + maxIndexR + ", minIndexR amount = " + minIndexR);
			System.out.println("maxIndexR index = " + maxIndexRin + ", minIndexR index = " + minIndexRin);
			//search for G min index and max index:
			int maxIndexG = indexAmountArrayG[0];
			int minIndexG = indexAmountArrayG[0];
			int maxIndexGin = 0;
			int minIndexGin = 0;
			for(int j = 0; j < sizeOfVectorSpace[1]; j++)
			{
				if (maxIndexG < indexAmountArrayG[j]) 
				{
					maxIndexG = indexAmountArrayG[j];
					maxIndexGin = j;
				}
					
				if (minIndexG > indexAmountArrayG[j]) 
				{
					minIndexG = indexAmountArrayG[j];
					minIndexGin = j;
				}
			}
			System.out.println("maxIndexG amount = " + maxIndexG + ", minIndexG amount = " + minIndexG);
			System.out.println("maxIndexG index = " + maxIndexGin + ", minIndexG index = " + minIndexGin);
			//search for B min index and max index:
			int maxIndexB = indexAmountArrayB[0];
			int minIndexB = indexAmountArrayB[0];
			int maxIndexBin = 0;
			int minIndexBin = 0;
			for(int j = 0; j < sizeOfVectorSpace[2]; j++)
			{
				if (maxIndexB < indexAmountArrayB[j]) 
				{
					maxIndexB = indexAmountArrayB[j];
					maxIndexBin = j;
				}
					
				if (minIndexB > indexAmountArrayB[j]) 
				{
					minIndexB = indexAmountArrayB[j];
					minIndexBin = j;
				}
			}
			System.out.println("maxIndexB amount = " + maxIndexB + ", minIndexB amount = " + minIndexB);
			System.out.println("maxIndexB index = " + maxIndexBin + ", minIndexB index = " + minIndexBin);
			
			
			
			//Step4. Output the images. Look up the codebook
			int[][][] myRGBArrayOut = new int[352][288][3];
			
			
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayOut[x][y][0] = myRGBIndex[x][y][0] * rgb_interval[0] + rgb_interval[0]/2;//r
					myRGBArrayOut[x][y][1] = myRGBIndex[x][y][1] * rgb_interval[1] + rgb_interval[1]/2;//g
					myRGBArrayOut[x][y][2] = myRGBIndex[x][y][2] * rgb_interval[2] + rgb_interval[2]/2;//b
				}
			}		
			// display output image1			
			ind = 0;
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					//int pix = 0xff000000 | ((myRGBArrayOut[x][y][0] & 0xff) << 16) | ((myRGBArrayOut[x][y][1] & 0xff) << 8) | (myRGBArrayOut[x][y][2] & 0xff);NOT USE,3/4/2011
					int pix = ((a << 24) + (myRGBArrayOut[x][y][0] << 16) + (myRGBArrayOut[x][y][1] << 8) + myRGBArrayOut[x][y][2]);// good	
					//int pix = 0;//black
					myImgOutput1.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					ind++;
				}
			}
			
			//display output image2, red difference, smallest, so use less cells for red
			int[][][] myRGBArrayR = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayR[x][y][0] = (myRGBIndex[x][y][0] * rgb_interval[0] + rgb_interval[0]/2) - myRGBArrayIn[x][y][0];//r
					myRGBArrayR[x][y][1] = 0;//g
					myRGBArrayR[x][y][2] = 0;//b
				}
			}			
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					
					//int pix = 0xff000000 | ((myRGBArrayR[x][y][0] & 0xff) << 16) | ((myRGBArrayR[x][y][1] & 0xff) << 8) | (myRGBArrayR[x][y][2] & 0xff);NOT USE,3/4/2011
					int pix = ((a << 24) + (myRGBArrayR[x][y][0] << 16) + (myRGBArrayR[x][y][1] << 8) + myRGBArrayR[x][y][2]);// good	
					//int pix = 0;//black
					myImgOutputR.setRGB(x,y,pix);//Sets a pixel in this BufferedImage to the specified RGB value.
					
				}
			}
			
			//display output image3, green difference
			int[][][] myRGBArrayG = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{			 
					myRGBArrayG[x][y][0] = 0;//r
					myRGBArrayG[x][y][1] = (myRGBIndex[x][y][1] * rgb_interval[1] + rgb_interval[1]/2) - myRGBArrayIn[x][y][1];//g
					myRGBArrayG[x][y][2] = 0;//b
				}
			}
			for(int y = 0; y < height; y++)
			{
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					int pix = ((a << 24) + (myRGBArrayG[x][y][0] << 16) + (myRGBArrayG[x][y][1] << 8) + myRGBArrayG[x][y][2]);
					myImgOutputG.setRGB(x, y, pix);
				}
			}
			
			//display output image4, blue difference
			int[][][] myRGBArrayB = new int[352][288][3];
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{
					myRGBArrayB[x][y][0] = 0;//r
					myRGBArrayB[x][y][1] = 0;//g
					myRGBArrayB[x][y][2] = (myRGBIndex[x][y][2] * rgb_interval[2] + rgb_interval[2]/2) - myRGBArrayIn[x][y][2];//b
				}
			}
			for(int y = 0; y < height; y++)
			{				
				for(int x = 0; x < width; x++)
				{
					byte a = 0;
					int pix = ((a << 24) + (myRGBArrayB[x][y][0] << 16) + (myRGBArrayB[x][y][1] << 8) + myRGBArrayB[x][y][2]);
					myImgOutputB.setRGB(x, y, pix);
				}
			}
			
			is.close();
			
		
			
			
	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	    
	    // Use a label to display the image
	    //JFrame frame = new JFrame();
	    //JLabel label = new JLabel(new ImageIcon(img));
	    JFrame myFrameInput = new JFrame("Input frame");	    
	    JLabel myLabelInput = new JLabel(new ImageIcon(myImgInput));
	    JFrame myFrameOutput1 = new JFrame("Output frame1");
	    JLabel myLabelOutput1 = new JLabel(new ImageIcon(myImgOutput1));
	    JFrame myFrameOutputR = new JFrame("Output frameR");
	    JLabel myLabelOutputR = new JLabel(new ImageIcon(myImgOutputR));
	    JFrame myFrameOutputG = new JFrame("Output frameG");
	    JLabel myLabelOutputG = new JLabel(new ImageIcon(myImgOutputG));
	    JFrame myFrameOutputB = new JFrame("Output frameB");
	    JLabel myLabelOutputB = new JLabel(new ImageIcon(myImgOutputB));
	    
	    //frame.getContentPane().add(label, BorderLayout.CENTER); // using JFrame you need to add the child to the JFrame's content pane:
																//	frame.getContentPane().add(child);
	    //frame.pack(); //Size the frame.
	    //frame.setVisible(true); //Show it.
	    myFrameInput.getContentPane().add(myLabelInput, BorderLayout.CENTER);
	    myFrameInput.pack(); //Size the frame.
	    myFrameInput.setVisible(true); //Show it.
	    
	    myFrameOutput1.getContentPane().add(myLabelOutput1, BorderLayout.CENTER);
	    myFrameOutput1.pack();
	    //myFrameOutput1.setLocation(400, 0);
	    myFrameOutput1.setVisible(true);
	    
	    myFrameOutputR.getContentPane().add(myLabelOutputR, BorderLayout.CENTER);
	    myFrameOutputR.pack();
	    myFrameOutputR.setLocation(400, 0);
	    myFrameOutputR.setVisible(true);
	    
	    myFrameOutputG.getContentPane().add(myLabelOutputG, BorderLayout.CENTER);
	    myFrameOutputG.pack();
	    myFrameOutputG.setLocation(0, 350);
	    myFrameOutputG.setVisible(true);
	    
	    myFrameOutputB.getContentPane().add(myLabelOutputB, BorderLayout.CENTER);
	    myFrameOutputB.pack();
	    myFrameOutputB.setLocation(400, 350);
	    myFrameOutputB.setVisible(true);
	}
	

   }
   
   
  
}

